#!/bin/bash
#https://www.cnblogs.com/wanghs8/p/17239160.html

# 生成公钥和私钥
ssh-keygen

# 将公钥复制到目标主机上
read -p "请输入目标主机的IP地址或域名：" host
read -p "请输入目标主机的用户名：" user
ssh-copy-id -i ~/.ssh/id_rsa.pub $user@$host

# 配置目标主机的ssh服务
echo "RSAAuthentication yes" >> /etc/ssh/sshd_config
echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config
echo "AuthorizedKeysFile .ssh/authorized_keys" >> /etc/ssh/sshd_config
systemctl restart sshd

# 将私钥添加到SSH代理中
ssh-add ~/.ssh/id_rsa

echo "SSH免密登录配置完成！"